package cn.edu.sdjzu.xg11.dao;

import java.util.Collection;
import java.util.TreeSet;

import org.springframework.stereotype.Repository;

import cn.edu.sdjzu.xg11.domain.Department;
import cn.edu.sdjzu.xg11.domain.School;
@Repository
public final class DepartmentDao {
	private static Collection<Department> departments;
	static {
		School managementSchool = null;

		managementSchool = new SchoolDao().getSchool(2);

		departments = new TreeSet<Department>();
		Department department = new Department(1, "工程管理", "0201", "",
				managementSchool);
		departments.add(department);
		departments
				.add(new Department(2, "信息管理", "0202", "", managementSchool));
		departments
				.add(new Department(3, "工程造价", "0203", "", managementSchool));
		departments
				.add(new Department(4, "工业工程", "0204", "", managementSchool));
	}

	public Collection<Department> getDepartments() {
		return DepartmentDao.departments;
	}

	public Department getDepartment(Integer id) {
		Department desiredDepartment = null;
		for (Department department : departments) {
			if (id.equals(department.getId())) {
				desiredDepartment = department;
				break;
			}
		}
		return desiredDepartment;
	}

	public boolean updateDepartment(Department department) {
		departments.remove(department);
		return departments.add(department);
	}

	public boolean addDepartment(Department department) {
		return departments.add(department);
	}

	public boolean deleteDepartment(Integer id) {
		Department department = this.getDepartment(id);
		return this.deleteDepartment(department);
	}

	public boolean deleteDepartment(Department department) {
		return departments.remove(department);
	}

	public static void main(String[] args) {
		DepartmentDao dao = new DepartmentDao();
		Collection<Department> departments = dao.getDepartments();
		display(departments);
		// Department department = new Department(2,"副教授","02","");
		// department.setNo("02x");
		// dao.updateDepartment(department);
		// display(departments);
		//
		// dao.deleteDepartment(department);
		// dao.deleteDepartment(3);
		// display(departments);
		// dao.addDepartment(new Department(2,"工程师","04",""));
		// display(departments);
		// System.out.println("2="+dao.getDepartment(2));
	}

	private static void display(Collection<Department> departments) {
		for (Department department : departments) {
			System.out.println(department);
		}
	}

}
